//蛇蛇的类
(function(window) {
    class SnakeBody extends Render { //蛇的每一节身体
        constructor(width, height, bgColor, x, y) {
            super(width, height, bgColor, x, y);
            this.width = width;
            this.height = height;
            this.x = x;
            this.y = y;
            this.bgColor = bgColor;
        }
    }
    class Snake {
        constructor(width, height, initLength, direction, speed) {
            this.width = width || 0.15;
            this.height = height || 0.15;
            this.initLength = initLength || 3;
            this.direction = direction || 'right';
            this.speed = speed || 1;
            this.rank = 1;
            this.body = [];
            for (let i = 0; i < this.initLength; i++) {
                this.body[i] = new SnakeBody(this.width, this.height, '#333', this.initLength - i, 1);
            }
        }
        initial = function(width, height, initLength, direction, speed) {
                this.width = width || 0.15;
                this.height = height || 0.15;
                this.initLength = initLength || 3;
                this.direction = direction || 'right';
                this.speed = speed || 1;
                this.rank = 1;
                this.body = [];
                for (let i = 0; i < this.initLength; i++) {
                    this.body[i] = new SnakeBody(this.width, this.height, '#333', this.initLength - i, 1);
                }
            }
            //渲染蛇蛇的方法
        render = function() {
                for (let i = 0; i < this.body.length; i++) {
                    this.body[i].render();
                }
            }
            //移除蛇蛇的方法
        remove = function() {
                for (let i = 0; i < this.body.length; i++) {
                    this.body[i].remove();
                }
            }
            //蛇蛇移动的方法
        move = function() {
            for (let i = this.body.length - 1; i > 0; i--) { //蛇头之外的身体移动到前一节身体的位置
                this.body[i].x = this.body[i - 1].x;
                this.body[i].y = this.body[i - 1].y;
            }
            switch (this.direction) { //蛇的移动方向决定蛇头的移动方式
                case 'right':
                    this.body[0].x++;
                    break;
                case 'left':
                    this.body[0].x--;
                    break;
                case 'up':
                    this.body[0].y--;
                    break;
                case 'down':
                    this.body[0].y++;
                    break;
            }
        }
        grow = function() { //蛇蛇长大的方法
            let temp = new SnakeBody(this.body[0].width, this.body[0].height, this.body[this.body.length - 1].bgColor, this.body[this.body.length - 1].x, this.body[this.body.length - 1].y, );
            this.body.push(temp);
        }
    }
    window.Snake = Snake;
}(window))